package com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.activity.act.personal.PersonalActivity;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.ACache;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @description 计时器
 * Created by 安帅 on 2016/5/26 17:50.
 * PackageName com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.
 * ProjectName mlzmall_android.
 */
public class TimeUtil extends TimerTask {

    private Context context;

    public TimeUtil(Context context) {
        super();
        this.context = context;
    }

    protected void executeRequest(Request<?> request) {
        request.setRetryPolicy(new DefaultRetryPolicy(10000, 0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        VolleyRequestManager.addRequest(request, this);
    }

    protected Response.ErrorListener errorListener() {
        return new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                ToastUtils.showToast(context, VolleyErrorHelper.getMessage(error, context));
            }
        };
    }

    private static final String TGA = "TimeUtil";

    @Override
    public void run() {

        ACache cache = ACache.get(context);
        String sessiongkey = cache.getAsString("sessionKeyLogin");
        String phoneid = cache.getAsString("phoneid");
        String telephony = cache.getAsString("telephony");
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> map = new HashMap();
        map.put("telephone", phoneid);
        map.put("deviceId", telephony);
        map.put("randomKey", sessiongkey);
        HttpEntity entity = new HttpEntity(map);
        try {
            ResponseEntity result = restTemplate.exchange(URL.CUSTOMER.LOGIN_RETURN, HttpMethod.POST, entity, String.class);
            JSONObject object = new JSONObject(result.getBody().toString());
            String sessionKey = object.optString("data");
            if (sessionKey != null) {
                cache.put("sessionKeyLogin", sessionKey);
            }

        } catch (Exception e) {
        }
    }
}